<?php

declare(strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Controller\Aliyun;

use App\Utils\Result;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\DbConnection\Db;

/**
 * @AutoController(prefix="vod")
 * Class AliyunVodNotify
 * @package App\Controller\Aliyun
 */
class AliyunVodNotify
{
    /**
     * 上传成功后的异步通知
     */
    public function notify(RequestInterface $request)
    {
        $result= $request->all();
        if(empty($result)){
            return false;
        }
        $VideoId=$result["VideoId"];
        $EventType=$result["EventType"];
        $Extend = json_decode($result['Extend'],true);
        if($result['Status']=='success'){
            //转码完成 有转码

            if($EventType=='TranscodeComplete'){
                //返回的两个数组偶尔顺序不一样
                if($result["StreamInfos"][0]['Format']=='m3u8'){
                    $m3u8=$result["StreamInfos"][0]['FileUrl'];
                }else if($result["StreamInfos"][0]['Format']=='mp4'){
                    $mp4=$result["StreamInfos"][0]['FileUrl'];
                }
                if(isset($result["StreamInfos"][1]) && $result["StreamInfos"][1]['Format']=='m3u8'){
                    $m3u8=$result["StreamInfos"][1]['FileUrl'];
                }else if(isset($result["StreamInfos"][1]) && $result["StreamInfos"][1]['Format']=='mp4'){
                    $mp4=$result["StreamInfos"][1]['FileUrl'];
                }
                //根据环境存储到不同的数据库
                switch ($Extend['env']){
                    case 'dev':
                        $vod = Db::connection('default')->select('SELECT id FROM for_aliyunvod where video_id=?',[$VideoId]);
                        Db::connection('default')->update('Update for_aliyunvod set m3u8=?,stream_infos=?,upload_status=? where id=?',[$m3u8,json_encode($result["StreamInfos"]),4,$vod[0]['id']]);
                        break;
                    default:
                        $vod = Db::connection('default')->select('SELECT id FROM for_aliyunvod where video_id=?',[$VideoId]);
                        Db::connection('default')->update('Update for_aliyunvod set m3u8=?,stream_infos=?,upload_status=? where id=?',[$m3u8,json_encode($result["StreamInfos"]),4,$vod[0]['id']]);
                        break;
                }
            }

            //上传完成
            if($EventType=='FileUploadComplete'){
                $path=$result['FileUrl'];
                //根据环境存储到不同的数据库
                switch ($Extend['env']){
                    case 'dev':
                        Db::connection('default')->update('Update for_aliyunvod set path=?,upload_status=? where video_id=?',[$result['FileUrl'],2,$VideoId]);
                        break;
                    default:
                        Db::connection('default')->update('Update for_aliyunvod set path=?,upload_status=? where video_id=?',[$result['FileUrl'],2,$VideoId]);
                        break;
                }
            }
        }

    }

}
